

public class ListStack implements Stack {

	private LinkedList list;

	
	public ListStack() {
		list = new LinkedList();
	}

	
	public void push( Object x ) {
		list.insert( x, list.getLast() );
	}


	public Object top() {
		if ( isEmpty() ) {
			throw new IllegalStateException();
		}
		ListNode last = list.getLast();
		return last.getElement();
	}

	
	public Object pop() {
		if ( isEmpty() ) {
			throw new IllegalStateException();
		}
		ListNode last = list.getLast();
		Object x = last.getElement();
		list.remove( last );
		return x;
	}


	public boolean isEmpty() {
		return list.isEmpty();
	}


	public void clear() {
		list.clear();
	}

}
